using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using Infragistics.Win.UltraWinTree;
using RetailAnalytic.Core;


namespace RetailAnalytic.Tools.GeneratorLabel
{
    public partial class Form1 : Form
    {
        private int i;
        private int f;
        private DataTable _T_AllText;
        private DataTable _T_AllFile;
        private Dictionary<int, string> _All_NeedFiles;
        
        public Form1()
        {
            InitializeComponent();
            InitUtData();
            this.WindowState = FormWindowState.Maximized;
        }
        private void InitUtData()
        {
            propertyGrid1.SelectedObject = Properties.Settings.Default;
            this.ultraComboEditor1.Items.Add(".designer.cs");
            this.ultraComboEditor1.Items.Add(".cs");
            this.ultraComboEditor1.Value = this.ultraComboEditor1.Items[0];
            _T_AllText = new DataTable("All Text");
            this._T_AllText.Columns.Add("id", typeof(int));
            this._T_AllText.Columns.Add("NameFile", typeof(string));
            this._T_AllText.Columns.Add("CurrentCulture", typeof(string));
            this._T_AllText.Columns.Add("NameSpace", typeof(string));
            this._T_AllText.Columns.Add("NameClass", typeof(string));
            this._T_AllText.Columns.Add("NameObject", typeof(string));
            this._T_AllText.Columns.Add("ObjectString", typeof(string));
            this._T_AllText.Columns.Add("TextObject", typeof(string));
            

            _T_AllFile = new DataTable("All File");
            this._T_AllFile.Columns.Add("id", typeof(int));
            this._T_AllFile.Columns.Add("NamePath", typeof(string));
            this._T_AllFile.Columns.Add("NameFileName", typeof(string));
            this.utGrid.AfterCellActivate += new EventHandler(utGrid_AfterCellActivate);
            this.utGrid1.AfterCellActivate += new EventHandler(utGrid_AfterCellActivate); 
        }
        void utGrid_AfterCellActivate(object sender, EventArgs e)
        {
            Infragistics.Win.UltraWinGrid.UltraGrid ug = (Infragistics.Win.UltraWinGrid.UltraGrid)sender;
            ug.ActiveCell.Activation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            
             i = 1;
            f = 1;
            _All_NeedFiles = new Dictionary<int, string>();
            this._T_AllText.Clear();
            //            string filename = this.textBox1.Text;
            string dirname = this.textBox1.Text;
            DiscoveryFiles(dirname);
            System.Collections.IEnumerator IE = _All_NeedFiles.Keys.GetEnumerator();
            while (IE.MoveNext())
            {
                int key = int.Parse(IE.Current.ToString());
                TestFile(_All_NeedFiles[key]);
            }
            this.utGrid.DataSource = this._T_AllText;
        }
        private void DiscoveryFiles(string _Directory)
        {
            DirectoryInfo dr = new DirectoryInfo(_Directory);
            FileInfo[] masFiles = dr.GetFiles();
            foreach (FileInfo fl in masFiles)
            {
                if ((fl.Name.ToLower().Contains(this.ultraComboEditor1.Value.ToString())) && (!fl.Name.ToLower().Contains(".dll")))
                //if ((fl.Name.ToLower().Contains(".cs")) && (!fl.Name.ToLower().Contains(".dll")))
                {
                    _All_NeedFiles.Add(f, fl.FullName);
                    f++;
                }
            }
            DirectoryInfo[] masDirectorys = dr.GetDirectories();
            foreach (DirectoryInfo fl in masDirectorys)
            {
                DiscoveryFiles(fl.FullName);
            }
        }
        private void TestFile(string FileName)
        {
            
            string filename = FileName;
            FileInfo fi = new FileInfo(filename);

            StreamReader fr = new StreamReader(filename, Encoding.GetEncoding(1251));
            string line = "";
            string nameClass = "";
            string NameSpace = "";
            string constStr = "partial class ";
            while (line != null)
            {
                line = fr.ReadLine();
                if (line != null)
                {
                    if (line.Contains("namespace"))
                    {
                        string temp = line;
                        temp = temp.Replace("namespace", "");
                        NameSpace = temp.Replace(" ", "");
                    }
                    if (line.Contains(constStr))
                    {
                        string temp = line;
                        temp = temp.Replace(constStr, "");
                        nameClass = temp.Replace(" ", "");
                    }
                    if (nameClass != "")
                    {
                        if ((line.Contains(".Text")) && (line.Contains("=")))
                        {
                            string Line = line;
                            string first = Line.Substring(0, Line.IndexOf("=")).Replace(" ", "");
                            if ((!first.Contains("\"")) && (!first.Contains("if")) && (first.Contains(".Text")))
                            {
                                int r = first.IndexOf(".Text");
                                string test = first.Substring(r);
                                if (test == ".Text")
                                {
                                    string second = Line.Substring(Line.IndexOf("=") + 1);
                                    if ((second.Contains("\"")) && !(second.Contains("?")))
                                    {
                                        this._T_AllText.BeginInit();
                                        DataRow row = this._T_AllText.NewRow();
                                        row["id"] = i.ToString();
                                        row["NameFile"] = fi.Name;
                                        row["CurrentCulture"] = Properties.Settings.Default.CurrentCulture.DisplayName;
                                            
                                        row["NameSpace"] = NameSpace;
                                        row["NameClass"] = nameClass;
                                        row["NameObject"] = first.Replace(".Text","").Replace("this.","");
                                        row["ObjectString"] = first;
                                        row["TextObject"] = second.Replace(" \"", "").Replace("\"", "").Replace(";", "");
                                        
                                        this._T_AllText.Rows.Add(row);
                                        this._T_AllText.AcceptChanges();
                                        this._T_AllText.EndInit();
                                        i++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
        }
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            fd.SelectedPath = this.textBox1.Text;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = fd.SelectedPath;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int t=1;
            DataTable _All_TextMessage = new DataTable("All Text");
            _All_TextMessage.Columns.Add("id", typeof(int));
            _All_TextMessage.Columns.Add("TextObject", typeof(string));
            _All_TextMessage.Columns.Add("NameFile", typeof(string));
            _All_TextMessage.Columns.Add("NameObject", typeof(string));
            UniqueConstraint uniq = new UniqueConstraint(_All_TextMessage.Columns["TextObject"]);
            _All_TextMessage.Constraints.Add(uniq);
            _All_TextMessage.PrimaryKey = new System.Data.DataColumn[] { _All_TextMessage.Columns["TextObject"] };
            _All_TextMessage.BeginInit();
            foreach (DataRow row in _T_AllText.Rows)
            {
                DataRow r = _All_TextMessage.Rows.Find(row["TextObject"].ToString());
                if (r == null)
                {
                    DataRow nrow = _All_TextMessage.NewRow();
                    nrow["id"] = t.ToString();
                    nrow["TextObject"] = row["TextObject"].ToString();
                    nrow["NameFile"] = row["NameFile"].ToString();
                    nrow["NameObject"] = row["NameObject"].ToString();
                    t++;
                    _All_TextMessage.Rows.Add(nrow);
                    _All_TextMessage.AcceptChanges();
                }
                else
                {
                    r["NameFile"] = r["NameFile"].ToString() + " ; " + row["NameFile"].ToString();
                    r["NameObject"] = r["NameObject"].ToString() + " ; " + row["NameObject"].ToString();
                    _All_TextMessage.AcceptChanges();
                }
            }
            _All_TextMessage.EndInit();
            this.utGrid1.DataSource = _All_TextMessage;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            i = 1;
            this._T_AllText.Clear();
            TestFile(textBox2.Text);
            this.ultraGrid1.DataSource = this._T_AllText;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.InitialDirectory = new FileInfo(this.textBox2.Text).DirectoryName;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = fd.FileName;
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            string nameFile = Application.StartupPath + "\\" + Properties.Settings.Default.CurrentCulture.DisplayName + ".cs";
            StreamWriter fr = new StreamWriter(nameFile, false, Encoding.GetEncoding(1251));
            fr.WriteLine("public static class GetLabel");
            fr.WriteLine("{");


            fr.WriteLine("#region " + Properties.Settings.Default.CurrentCulture.DisplayName);
            foreach (DataRow row in this._T_AllText.Rows)
            {
                fr.WriteLine("      public static string " + Properties.Settings.Default.CurrentCulture.DisplayName +
                    "_" +
                    row["NameClass"].ToString()+
                    "_"+
                    row["NameObject"].ToString()
                    );

                fr.WriteLine("      {");
                fr.WriteLine("          get{ return \"" + row["TextObject"].ToString() + "\" ; }");
                fr.WriteLine("      }");
            }
            fr.WriteLine("#endregion " + Properties.Settings.Default.CurrentCulture.DisplayName);


            fr.WriteLine("}");
            fr.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {

        }
    }
}